package page

import (
	"goProject/page/api"
	"net/http"
)

func Index(w http.ResponseWriter, r *http.Request) {
	if r.Method == "OPTIONS" {
		return
	}
	if r.Method == "GET" {
		if r.FormValue("key") == "" {
			w.Header().Set("Content-Type", "text/html; charset=utf-8")
			w.Write([]byte(indexHtmlContent))
			return
		}
		api.Code(w, r)
		return
	}
	if r.Method == "POST" {
		api.Upload(w, r)
		return
	}
}

var indexHtmlContent = `
<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<script src="https://ruige.fun/static/js/axios.min.js"></script>
		<script src="https://ruige.fun/static/js/vue@2.6.14.js"></script>
		<title>分享代码</title>
		<style>.r-fx-col {display: flex;flex-direction: column;}</style>
	</head>
	<body>
		<div class="r-fx-col" id="app">
			<div style="display: flex;" v-if="codeURL === ''">
				<button style="font-size: 1.5em;flex: 1;" @click="Upload" :disabled="content === '' || buttonText != '提交分享'">{{buttonText}}</button>
			</div>
			<div style="display: flex;" v-if="codeURL">
				<input disabled style="font-size: 1.5em;flex: 1;" v-model="codeURL"/>
			</div>
			<div style="display: flex;margin: 8px;">
				<select v-model="lang" style="font-size: 1.5em;flex: 1;margin-right: 8px;">
					<option value="actionscript">ActionScript</option>
					<option value="apacheconf">ApacheConf</option>
					<option value="applescript">AppleScript</option>
					<option value="bash">Bash</option>
					<option value="basic">Basic</option>
					<option value="c">C</option>
					<option value="clike">C Like</option>
					<option value="cmake">Cmake</option>
					<option value="cpp">C++</option>
					<option value="csharp">C#</option>
					<option value="css">CSS</option>
					<option value="css-extras">CSS-extras</option>
					<option value="csv">CSV</option>
					<option value="django">Django</option>
					<option value="docker">Docker</option>
					<option value="erlang">Erlang</option>
					<option value="fsharp">F#</option>
					<option value="git">Git</option>
					<option value="go">GO</option>
					<option value="go-module">GO-module</option>
					<option value="gradle">Gradle</option>
					<option value="graphql">Graphql</option>
					<option value="http">Http</option>
					<option value="ini">Ini</option>
					<option value="java">Java</option>
					<option value="javascript">JavaScript</option>
					<option value="jq">jQuery</option>
					<option value="json">Json</option>
					<option value="json5">Json5</option>
					<option value="jsonp">Jsonp</option>
					<option value="jsx">Jsx</option>
					<option value="julia">Julia</option>
					<option value="kotlin">Kotlin</option>
					<option value="less">Less</option>
					<option value="lisp">Lisp</option>
					<option value="log">Log</option>
					<option value="lolcode">Lolcode</option>
					<option value="lua">Lua</option>
					<option value="makefile">Makefile</option>
					<option value="markdown">Markdown</option>
					<option value="markup">Markup</option>
					<option value="matlab">Matlab</option>
					<option value="mongodb">MongoDB</option>
					<option value="nasm">Nasm</option>
					<option value="nginx">Nginx</option>
					<option value="objectivec">Objective C</option>
					<option value="pascal">Pascal</option>
					<option value="perl">Perl</option>
					<option value="php-extras">Php-extras</option>
					<option value="php">PHP</option>
					<option value="powershell">Powershell</option>
					<option value="protobuf">Protobuf</option>
					<option value="python">Python</option>
					<option value="r">R</option>
					<option value="rest">Rest</option>
					<option value="ruby">Ruby</option>
					<option value="rust">Rust</option>
					<option value="sas">Sas</option>
					<option value="sass">Sass</option>
					<option value="scala">Scala</option>
					<option value="scheme">Scheme</option>
					<option value="scss">Scss</option>
					<option value="smarty">Smarty</option>
					<option value="sql">SQL</option>
					<option value="swift">Swift</option>
					<option value="systemd">Systemd</option>
					<option value="tsx">Tsx</option>
					<option value="typescript">TypeScript</option>
					<option value="wiki">Wiki</option>
					<option value="yaml">Yaml</option>
				</select>
				<select v-model="timeout_second" style="font-size: 1.5em;flex: 1;margin-left: 8px;">
					<option :value="180">3 分钟</option>
					<option :value="600">10 分钟</option>
					<option :value="1800">30 分钟</option>
					<option :value="3600">1 小时</option>
					<option :value="7200">2 小时</option>
					<option :value="14400">4 小时</option>
					<option :value="28800">8 小时</option>
					<option :value="43200">12 小时</option>
					<option :value="86400">1 天</option>
					<option :value="172800">2 天</option>
					<option :value="259200">3 天</option>
					<option :value="432000">5 天</option>
					<option :value="604800">7 天</option>
				</select>
			</div>
			<textarea v-model="content" style="height: 80vh;resize: none;"></textarea>
		</div>
		<script type="text/javascript">
			var app = new Vue({
				el:"#app",
				data:{
					lang: "go",
					timeout_second: 3600,
					content: "",
					buttonText: "提交分享",
					codeURL: ''
				},
				methods:{
					Upload:function(){
						let _this = this;
						axios.post('/code', {
							lang: _this.lang,
							timeout_second: Number(_this.timeout_second)||600,
							content: _this.content,
						}, {
							headers: {'Content-Type': 'application/json; charset=utf-8'}
						})
						.then(function (response) {
							if(response.data != null){
								if(response.data.code != 200){
									_this.buttonText = "提交分享失败：" + response.data.msg;
								}else{
									_this.codeURL = window.location.origin  + "/code?key=" + response.data.data.data;
									_this.buttonText = "提交分享成功";
									window.open(_this.codeURL);
								}
							}else{
								_this.buttonText = "提交分享失败：请求失败";
							}
						})
						.catch(function (error) {
							_this.buttonText = "提交分享失败：请求失败";
						});
					}
				}
			})
		</script>
	</body>
</html>
`
